package grex;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
import java.awt.Frame;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.StringTokenizer;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
//import javax.swing.filechooser.*;
public class FileManager {
  private BufferedReader in;
  private ArrayList names = new ArrayList();
  Frame parent;
  public FileManager(Frame parent) {
    this.parent = parent;
    init();




  }

  public void init() {
    JFileChooser jf = new JFileChooser();
    jf.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    if (JFileChooser.APPROVE_OPTION == jf.showOpenDialog(parent)) {
      File f = jf.getSelectedFile();
      File[] files = f.listFiles();
      StringTokenizer st;
      Object o;
      for (int i = 0; i < files.length; i++) {
        st = new StringTokenizer(files[i].getName(), ".");
        st.nextToken();
        if (st.hasMoreTokens() && st.nextToken().equalsIgnoreCase("result"))
        names.add(files[i]);
      }
      Collections.sort(names);
      saveFile();
    }

  }
  private void saveFile(){
        JFileChooser jf = new JFileChooser("Skriv namnet som filerna skall sammanstollas till");
        double tmpLength=0,antalFiler=0,lyckadeForenklingar=0;
        if(JFileChooser.APPROVE_OPTION == jf.showSaveDialog(parent)){
          try {
            PrintWriter p = new PrintWriter(new FileWriter(jf.getSelectedFile()));
            p.println("Name\tTrain\tVal\tTest\tLength\tSimplyfiedLength");
            StringTokenizer st;
            for (Iterator i = names.iterator(); i.hasNext(); ) {
              antalFiler++;
              File file = (File)i.next();
              StringTokenizer namn = new StringTokenizer(file.getName(),".");
              p.write(namn.nextToken()+"\t");
              BufferedReader r = new BufferedReader(new FileReader(file));
              r.readLine();r.readLine();
              //Los in Train,val,test,length
              String tmpS;
              for (int j= 0;j<4 ;j++ ) {
                st=new StringTokenizer(r.readLine()," ");
                st.nextToken();
                tmpS=st.nextToken();
                p.write(tmpS+"\t");
                if(j==3)
                  tmpLength=Double.parseDouble(tmpS);
              }
              r.readLine();r.readLine();r.readLine();r.readLine();r.readLine();
              st = new StringTokenizer(r.readLine()," ");              //Los in longd efter forenkling
              st.nextToken();
              if(Double.parseDouble(st.nextToken())==1){//Forenklingen lyckades
                lyckadeForenklingar++;
                r.readLine();
                r.readLine();
                st = new StringTokenizer(r.readLine(), " ");
                st.nextToken();
                p.write(st.nextToken() + "\n");
              }else{
                p.write(tmpLength + "\n");
              }
              r.close();
            }
            p.println();
              p.println("Forenklingen lyckade:"+lyckadeForenklingar/antalFiler);
              p.close();
                        JOptionPane.showMessageDialog(parent,"The file was created.","Information",JOptionPane.INFORMATION_MESSAGE);
          }
          catch (Exception ex) {
            ex.printStackTrace();
          }

        }
  }
}
